package com.weixinlocaldbdecrypt.controller;

import com.weixinlocaldbdecrypt.util.Md5Util;
import com.weixinlocaldbdecrypt.util.WxFileParse;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * IndexController
 *
 * @author <a href="morse.jiang@foxmail.com">JiangWen</a>
 * @version 1.0.0, 2019/8/30 0030 16:00
 */
@Controller
@RequestMapping
public class IndexController {


  /**
   * 跳转到登陆页面
   *
   * @return
   */
  @GetMapping(value = "/index")
  public String index() {
    return "index";
  }



  @PostMapping(value = "/uin")
  @ResponseBody
  public ResponseEntity uin(HttpServletRequest request) throws Exception {
    return new ResponseEntity<>(WxFileParse.getUin(upload(request)), HttpStatus.OK);
  }

  @PostMapping(value = "/imei")
  @ResponseBody
  public ResponseEntity imei(HttpServletRequest request) throws Exception {
    Map<String,String> map = new HashMap<>(2);
    map.put("name","imei");
    map.put("value",WxFileParse.getImei(upload(request)));
    return new ResponseEntity<>(map, HttpStatus.OK);
  }

  @PostMapping(value = "/pwd")
  @ResponseBody
  public ResponseEntity pwd(HttpServletRequest request) throws Exception {
    String imei = request.getParameter("imei");
    String uin = request.getParameter("uin");
    String pwd = Md5Util.md5(imei + uin).substring(0, 7);
    return new ResponseEntity<>(pwd, HttpStatus.OK);
  }



  /**
   * 上传文件获取输入流
   * *
   *
   * @throws Exception
   */
  private InputStream upload(HttpServletRequest request) throws Exception {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    //检查form中是否有enctype="multipart/form-data"
    if (multipartResolver.isMultipart(request)) {
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
      //获取multiRequest 中所有的文件名
      Iterator iter = multiRequest.getFileNames();
      while (iter.hasNext()) {
        //一次遍历所有文件
        MultipartFile file = multiRequest.getFile(iter.next().toString());
        CommonsMultipartFile cFile = (CommonsMultipartFile) file;
        DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();
        return fileItem.getInputStream();
      }
    }
    return null;
  }


}
